package com.googlecode.grima.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.grima.base.GenericDao;
import com.googlecode.grima.base.GenericEntityService;
import com.googlecode.grima.base.GenericEntityServiceImpl;
import com.googlecode.grima.base.Page;
import com.googlecode.grima.base.SearchResult;
import com.googlecode.grima.base.SearchTemplate;
import com.googlecode.grima.dao.PartnerDao;
import com.googlecode.grima.domain.Partner;
import com.googlecode.grima.domain.PartnerCategory;

@Service("partnerService")
public class PartnerService extends GenericEntityServiceImpl<Partner, Long> implements GenericEntityService<Partner, Long> {

	protected PartnerDao partnerDao;

	@Autowired
	public void setPartnerDao(PartnerDao partnerDao) {
		this.partnerDao = partnerDao;
	}

	@Override
	public GenericDao<Partner, Long> getDao() {
		return partnerDao;
	}

	@Override
	public Partner getNew() {
		return new Partner();
	}

	@Override
	public Partner getNewWithDefaults() {
		return getNew();
	}
	
	@Override
	@Transactional
	public void save(Partner partner) {
		super.save(partner);
	}
	
	public SearchResult<Partner, Long> findAll(Page page){
		SearchResult<Partner, Long> ret = new SearchResult<Partner, Long>();
    SearchTemplate searchTemplate = new SearchTemplate();

		if (page != null){
			int pageNumber = page.getPageNumber();
			int pageSize = page.getPageSize();
	    int firstResult = ((pageNumber-1) * pageSize);
	    searchTemplate.setFirstResult(firstResult);
	    searchTemplate.setMaxResults(pageSize);
		}
		
		List<Partner> partners = find(searchTemplate);
		int count = findCount(searchTemplate);

		ret.setTotal(count);
		ret.setRows(partners);
		
		return ret;
	}
	
	public SearchResult<Partner, Long> findByParameters(String name, PartnerCategory category, Page page) {
		SearchResult<Partner, Long> ret = new SearchResult<Partner, Long>();
		
		SearchTemplate searchTemplate = new SearchTemplate();

		if (page != null){
			int pageNumber = page.getPageNumber();
			int pageSize = page.getPageSize();
			
	    int firstResult = ((pageNumber-1) * pageSize);
	    searchTemplate.setFirstResult(firstResult);
	    searchTemplate.setMaxResults(pageSize);
    }    
    
		if (StringUtils.isNotEmpty(name)){
    	searchTemplate.addCriterion(Restrictions.like("name", "%"+name+"%"));
    }

		if (category != null){
    	searchTemplate.addCriterion(Restrictions.eq("category", category));
    }
		
		List<Partner> partners = find(searchTemplate);
		int count = findCount(searchTemplate);

		ret.setTotal(count);
		ret.setRows(partners);
		
		return ret;
  }
	
	public void deleteById(Long id){
		deleteByPrimaryKey(id);
	}

}
